package com.datastructure.test.compressstring;

import java.util.HashMap;
import java.util.Map;

public class CompressString {

    public static void main(String[] args) {
        CompressString compressString = new CompressString();
        String str = "aa";
        String string = compressString.compressString(str);
        System.out.println(string);
    }

    public String compressString (String param) {
        // write code here
        param=param+"1";
        StringBuilder stringBuilder = new StringBuilder();
        String lastString="";
        int index=1;
        String[] split = param.split("");
        for (String s : split) {
            if(s.equals("1")){
                stringBuilder.append(lastString);
                if(index>1){
                    stringBuilder.append(index);
                }
                continue;
            }
            if (!s.equals(lastString)) {
                stringBuilder.append(lastString);
                if (index > 1) {
                    stringBuilder.append(index);
                    index=1;
                }
                lastString = s;
            } else {
                index++;
            }
        }
        return stringBuilder.toString();
    }

    public String compressString1 (String param) {
        // write code here
        //先把字符串转换为字符串数组
        if(param.length()==0){
            return "";
        }
        char[] c=param.toCharArray();
        StringBuilder result=new StringBuilder();//存放结果
        int count=1;
        for(int i=0;i<c.length-1;i++){
            if(c[i]==c[i+1]){
                count++;
            }else{
                result.append(c[i]);
                if(count>1){
                    result.append(count);
                }
                count=1;//将count重新置为1
            }
        }
        //字符串中最后一个字符
        result.append(c[c.length-1]);
        if(count>1){
            result.append(count);
        }
        return result.toString();

    }

    public String maxLexicographical (String num) {
        // write code here
        return num.replaceAll("0","1");
    }
}
